﻿using AMAP.Areas.Public.Models.Authentication;
using AMAP.Domain;
using AMAP.ServiceInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace AMAP.Areas.Public.Controllers
{
    public class AuthenticationController : Controller
    {
        IAuthenticationService AuthenticationService;

        public AuthenticationController(IAuthenticationService AuthenticationService)
        {
            this.AuthenticationService = AuthenticationService;
        }

        //
        // GET: /Public/Authentication/

        public ActionResult Index()
        {
            return LogOn();
        }

        public ActionResult LogOn()
        {
            PublicAuthenticationLoginViewModel Model = new PublicAuthenticationLoginViewModel();

            //Redirect the user if he is already logged
            if (!string.IsNullOrEmpty(User.Identity.Name))
            {
                string[] Roles = AuthenticationService.GetUserRoles(User.Identity.Name);

                if (Roles.Contains(EnumTypes.User.Client.ToString()))
                    return RedirectToRoute("Client_default", new { controller = "Home", action = "Index" });
                else if (Roles.Contains(EnumTypes.User.Supplier.ToString()))
                    return RedirectToRoute("Supplier_default", new { controller = "Home", action = "Index" });

                return RedirectToRoute("Admin_default", new { controller = "Home", action = "Index" });
            }
            return View(Model);
        }

        [HttpPost]
        public ActionResult LogOn(PublicAuthenticationLoginViewModel Model, string ReturnUrl)
        {
            if (ModelState.IsValid)
            {
                if (AuthenticationService.CheckAuthentication(Model.Email, Model.Password))
                {
                    FormsAuthentication.SetAuthCookie(Model.Email, Model.RememberMe);

                    if (Url.IsLocalUrl(ReturnUrl) && ReturnUrl.Length > 1 && ReturnUrl.StartsWith("/") && !ReturnUrl.StartsWith("//") && !ReturnUrl.StartsWith("/\\"))
                        return Redirect(ReturnUrl);


                    string[] Roles = AuthenticationService.GetUserRoles(Model.Email);

                    if (Roles.Contains(EnumTypes.User.Client.ToString()))
                        return RedirectToRoute("Client_default", new { controller = "Home", action = "Index" });
                    else if (Roles.Contains(EnumTypes.User.Supplier.ToString()))
                        return RedirectToRoute("Supplier_default", new { controller = "Home", action = "Index" });

                    return RedirectToRoute("Admin_default", new { controller = "Home", action = "Index" });
                }
                else
                {
                    Model.ErrorMessage = "Adresse e-mail ou mot de passe incorrect";
                }
            }
            return View(Model);
        }

        public ActionResult IsEmailAvailable(string Email)
        {
            return Json(AuthenticationService.IsEmailAvailable(Email), JsonRequestBehavior.AllowGet);
        }

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToRoute("Default", new { controller = "Home" });
        }
    }
}
